package com.wwh.furns.dao.impl;

import com.wwh.furns.dao.BasicDAO;
import com.wwh.furns.dao.MemberDAO;
import com.wwh.furns.entity.Member;

/**
 * @author Albert
 * @date 2024/5/10-21:11
 * @describe
 */
public class MemberDAOImpl extends BasicDAO<Member> implements MemberDAO{

    /**
     * 通过用户名查找返回一个Member
     * @param username 用户名
     * @return 如果找到，返回Member；如果找不到，返回null
     */
    @Override
    public Member queryMemberByUsername(String username) {
        String sql = "SELECT `id`,`username`,`password`,`email` FROM member WHERE `username`=?";
        return querySingle(sql, Member.class, username);
    }

    /**
     * 保存一个会员
     * @param member 一个会员
     * @return 如果保存成功，返回受影响行数；如果保存失败，返回-1
     */
    @Override
    public int saveMember(Member member) {
        String sql = "INSERT INTO member(`username`,`password`,`email`) VALUES(?,MD5(?),?);";
        return update(sql, member.getUsername(), member.getPassword(), member.getEmail());
    }

    /**
     * 通过用户名和密码返回member对象(alt+enter快速实现)
     * @param username 用户名
     * @param password 密码
     * @return 若存在则返回member对象，若不存在则返回null
     */
    @Override
    public Member queryMemberByUsernameAndPassword(String username, String password) {
        String sql = "SELECT `id`,`username`,`password`,`email` FROM member WHERE `username`=? AND `password`=MD5(?)";
        return querySingle(sql, Member.class, username, password);
    }
}
